package canss.webservice.controls;


import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import canss.encoder.DBStringEncoder;
import canss.supports.basic;
import canss.webservice.model.ContentDesc;


import weblogic.jdbc.SQLExceptionWrapper;


import java.io.Serializable;
import java.sql.Timestamp;

@ControlImplementation
public class ContentDescControlImpl implements ContentDescControl, Serializable
{
	private static final long serialVersionUID = -1701891600365486991L;

	@Control
	private BusinessSystemDB businesssystemDB;
	
	
	//
	// [2]
	public Boolean createRelativeContentDescription(String _cid) throws SQLExceptionWrapper
	{
		String tmp = DBStringEncoder.Server2DB(_cid);
		if(tmp==null)
			return false;
		int ret = businesssystemDB.initiateRelativeContentDescription(tmp, INITIATERELATIVECONTENTDESCRIPTIONSTATUS);
		return ret>0;
	}
	public Boolean deleteContentDescription(String _cid) throws SQLExceptionWrapper
	{
		String tmp = DBStringEncoder.Server2DB(_cid);
		if(tmp==null)
			return false;
		int ret = businesssystemDB.deleteContentDescription(_cid);
		return ret>0;
	}
	public Boolean updatePIDByCID(String _cid, String _newpid) throws SQLExceptionWrapper
	{
		if(basic.isEmpty(_cid) || basic.isEmpty(_newpid))
			return null;
		int ret = businesssystemDB.updatePIDByCID(DBStringEncoder.Server2DB(_cid), DBStringEncoder.Server2DB(_newpid));
		return ret>0;
	}
	public Boolean setContentStatus(String _cid, Integer _status) throws SQLExceptionWrapper, RuntimeException
	{
		if(_status<0)
			throw new RuntimeException("strange status");
		String tmp = DBStringEncoder.Server2DB(_cid);
		if(tmp==null)
			return false;
		int ret = businesssystemDB.setContentStatus(tmp, _status);
		return ret>0;
	}
	
	public ContentDesc getContentDescription(String _cid) throws SQLExceptionWrapper
	{
		if( !basic.isEmpty(_cid) )
		{
			ContentDesc[] contentdesclist = businesssystemDB.getContentDescByCID(DBStringEncoder.Server2DB(_cid));
			if(contentdesclist==null || contentdesclist.length==0)
				return null;
			for(int i=0; i<contentdesclist.length; i++)
			{
				if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()!=null)
					contentdesclist[i].setCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()));
				if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()!=null)
					contentdesclist[i].setCANSSCONTENTDESCRIPTION_VALIDATT_NAME(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()));
				if(contentdesclist[i].getTBLCID_CID()!=null)
					contentdesclist[i].setTBLCID_CID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLCID_CID()));
				if(contentdesclist[i].getTBLPID_PID()!=null)
					contentdesclist[i].setTBLPID_PID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLPID_PID()));
			}
			return contentdesclist[0]; 
			}
		return null;
	}
	public ContentDesc[] getContentDescriptionByPID(String _pid) throws SQLExceptionWrapper
	{
		if( !basic.isEmpty(_pid) )
		{
			ContentDesc[] contentdesclist = businesssystemDB.getContentDescByPID(DBStringEncoder.Server2DB(_pid));
			if(contentdesclist==null || contentdesclist.length==0)
				return null;
			for(int i=0; i<contentdesclist.length; i++)
			{
				if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()!=null)
					contentdesclist[i].setCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()));
				if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()!=null)
					contentdesclist[i].setCANSSCONTENTDESCRIPTION_VALIDATT_NAME(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()));
				if(contentdesclist[i].getTBLCID_CID()!=null)
					contentdesclist[i].setTBLCID_CID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLCID_CID()));
				if(contentdesclist[i].getTBLPID_PID()!=null)
					contentdesclist[i].setTBLPID_PID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLPID_PID()));
			}
			return contentdesclist;
		}
		return null;
	}
	public ContentDesc[] getContentDescriptionASCPID() throws SQLExceptionWrapper
	{
		ContentDesc[] contentdesclist = businesssystemDB.getContentDescASCPID();
		if(contentdesclist==null || contentdesclist.length==0)
			return null;
		for(int i=0; i<contentdesclist.length; i++)
		{
			if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()!=null)
				contentdesclist[i].setCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()));
			if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()!=null)
				contentdesclist[i].setCANSSCONTENTDESCRIPTION_VALIDATT_NAME(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()));
			if(contentdesclist[i].getTBLCID_CID()!=null)
				contentdesclist[i].setTBLCID_CID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLCID_CID()));
			if(contentdesclist[i].getTBLPID_PID()!=null)
				contentdesclist[i].setTBLPID_PID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLPID_PID()));
		}
		return contentdesclist;
	}
	public ContentDesc[] getContentDescWhichNoOwned() throws SQLExceptionWrapper
	{
		ContentDesc[] contentdesclist = businesssystemDB.getContentDescWhichNoOwned();
		if(contentdesclist==null || contentdesclist.length==0)
			return null;
		for(int i=0; i<contentdesclist.length; i++)
		{
			if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()!=null)
				contentdesclist[i].setCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()));
			if(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()!=null)
				contentdesclist[i].setCANSSCONTENTDESCRIPTION_VALIDATT_NAME(DBStringEncoder.DB2Server(contentdesclist[i].getCANSSCONTENTDESCRIPTION_VALIDATT_NAME()));
			if(contentdesclist[i].getTBLCID_CID()!=null)
				contentdesclist[i].setTBLCID_CID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLCID_CID()));
			if(contentdesclist[i].getTBLPID_PID()!=null)
				contentdesclist[i].setTBLPID_PID(DBStringEncoder.DB2Server(contentdesclist[i].getTBLPID_PID()));
		}
		return contentdesclist;

	}
	
	private static final String INITIALPRICECODE="KRW";
	public Boolean setPriceByCID(String _cid, Double _price) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.setPriceByCID(DBStringEncoder.Server2DB(_cid), _price, DBStringEncoder.Server2DB(INITIALPRICECODE));
		return ret>0;
	}
	public Boolean setValidDateByCID(String _cid, Timestamp _start, Timestamp _end) throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.setValiddateByCID(DBStringEncoder.Server2DB(_cid), _start, _end);
		return ret>0;
	}
	
	public int getContentStatusByCID(String _cid) throws SQLExceptionWrapper, RuntimeException
	{
		ContentDesc[] contentdesclist = businesssystemDB.getContentDescByCID(DBStringEncoder.Server2DB(_cid));
		if(contentdesclist!=null && contentdesclist.length>0)
			return contentdesclist[0].getCANSSCONTENTDESCRIPTION_STATUS();
		throw new RuntimeException();
	}

	public ContentDesc[] getContentDescByStatus(Integer _status) throws SQLExceptionWrapper
	{
		ContentDesc[] contentdesclist = businesssystemDB.getContentDescByStatus(_status);
		if(contentdesclist==null || contentdesclist.length==0)
			return null;
		for(ContentDesc contentdesc : contentdesclist)
			contentdesc.setTBLCID_CID(DBStringEncoder.DB2Server(contentdesc.getTBLCID_CID()));
		return contentdesclist;
	}
	public ContentDesc[] getContentDescByLocationFilteredStatus(Integer _locationfilter) throws SQLExceptionWrapper
	{
		ContentDesc[] contentdesclist = businesssystemDB.getContentDescByLocationFilteredStatus(_locationfilter.toString()+"__");
		if(contentdesclist==null || contentdesclist.length==0)
			return null;
		for(ContentDesc contentdesc : contentdesclist)
			contentdesc.setTBLCID_CID(DBStringEncoder.DB2Server(contentdesc.getTBLCID_CID()));
		return contentdesclist;
	}
}